﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _05ToUpperBetweenTags
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "In the <upcase>town</upcase> where I was born, Lived town a man who sailed to sea, And he told us of his life, In the land of <upcase>submarines</upcase>, So we sailed on to the   sun, Till we found the sea green, And we lived beneath the waves, In our yellow <upcase>submarine</upcase>, We all live in a yellow <upcase>submarine</upcase>, yellow <upcase>submarine</upcase>, yellow <upcase>submarine</upcase>, We all live in a yellow <upcase>submarine</upcase>, yellow <upcase>submarine</upcase>, yellow <upcase>submarine</upcase>.";
            StringBuilder upString = new StringBuilder();
            upString.Append(str);
            string openTag = "<upcase>";
            string closeTag = "</upcase>";
            int startIndex = 0;
            int endIndex = 0;
            for (int i = 0; i < str.Length - (closeTag.Length + openTag.Length); i++)
            {
                if (str.Substring(i, openTag.Length) == openTag)
                {
                    startIndex = i + 8;
                }
                if (str.Substring(i, closeTag.Length) == closeTag)
                {
                    endIndex = i;
                }

                if ((i == endIndex) && (i != 0))
                {
                    upString = upString.Replace(upString.ToString().Substring(startIndex, (endIndex - startIndex)),
                                                upString.ToString().Substring(startIndex, (endIndex - startIndex)).ToUpper(),
                                                endIndex - (endIndex - startIndex), (endIndex - startIndex));
                }
            }
            upString.Replace("<upcase>", "");
            upString.Replace("</upcase>", "");
            Console.WriteLine(str);
            Console.WriteLine("********************************************************************************");
            Console.WriteLine(upString);
        }
    }
}
